package com.didi.gulimall.listtener;

import com.alibaba.fastjson.JSON;
import com.didi.gulimall.common.constant.RabbitMqConstant;
import com.didi.gulimall.common.entity.LocalMqEntity;
import com.didi.gulimall.common.to.OrderBackVo;
import com.didi.gulimall.common.to.OrderSuccessVo;
import com.didi.gulimall.common.utils.LogUtils;
import com.didi.gulimall.ware.service.LocalMqService;
import com.didi.gulimall.ware.service.WareOrderTaskService;
import com.didi.gulimall.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @Author 10626
 * @Create 2021/6/4 11:30
 */
@Slf4j
@Component
public class WareConsumerMqListener {
    @Autowired
    private LocalMqService localMqService;
    @Autowired
    private WareOrderTaskService wareOrderTaskService;
    @Autowired
    private WareSkuService wareSkuService;

    @RabbitListener(queues = "order.release.ware.queue")
    public void receiveMessageWare(OrderBackVo orderBackVo, Message message, Channel channel) throws IOException {
        MessageProperties messageProperties = message.getMessageProperties();
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        String messageId = (String) headers.get("spring_returned_message_correlation");
        long deliveryTag = messageProperties.getDeliveryTag();
        log.info("库存解锁订单收到消息" + JSON.toJSONString(orderBackVo));
        LocalMqEntity localMqEntity = localMqService.getById(messageId);
        if (localMqEntity == null || localMqEntity.getStatus().equals(RabbitMqConstant.CONSUMED)) {
            //   该订单已经回滚
            channel.basicAck(deliveryTag, false);
            localMqService.updateMqStatus(messageId, RabbitMqConstant.CONSUMED);
        } else {
            if (localMqEntity.getRetries() < RabbitMqConstant.MAXIMUM_NUMBER_OF_RETRIES) {
                try {
                    wareOrderTaskService.orderBack(orderBackVo);
                    channel.basicAck(deliveryTag, false);
                    localMqService.updateMqStatus(messageId, RabbitMqConstant.CONSUMED);
                } catch (Exception e) {
                    log.error(LogUtils.getStackTraceInfo(e));
                    localMqService.updateMqStatusReg(messageId, localMqEntity.getRetries() + 1);
                    channel.basicReject(deliveryTag, true);
                }
            } else {
                // 消息丢弃
                channel.basicReject(deliveryTag, false);
            }
        }
    }

    @RabbitListener(queues = "order.paysuccess.ware.queue")
    public void paysuccessMessageWare(OrderSuccessVo orderSuccessVo, Message message, Channel channel) throws IOException {
        MessageProperties messageProperties = message.getMessageProperties();
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        String messageId = (String) headers.get("spring_returned_message_correlation");
        long deliveryTag = messageProperties.getDeliveryTag();
        log.info("订单收到消息" + JSON.toJSONString(orderSuccessVo));
        LocalMqEntity localMqEntity = localMqService.getById(messageId);
        if (localMqEntity == null || localMqEntity.getStatus().equals(RabbitMqConstant.CONSUMED)) {
            //   该订单已经回滚
            channel.basicAck(deliveryTag, false);
            localMqService.updateMqStatus(messageId, RabbitMqConstant.CONSUMED);
        } else {
            if (localMqEntity.getRetries() < RabbitMqConstant.MAXIMUM_NUMBER_OF_RETRIES) {
                try {
                    wareOrderTaskService.paysuccessDeductionInventory(orderSuccessVo);
                    channel.basicAck(deliveryTag, false);
                    localMqService.updateMqStatus(messageId, RabbitMqConstant.CONSUMED);
                } catch (Exception e) {
                    log.error(LogUtils.getStackTraceInfo(e));
                    localMqService.updateMqStatusReg(messageId, localMqEntity.getRetries() + 1);
                    channel.basicReject(deliveryTag, true);
                }
            } else {
                // 消息丢弃
                channel.basicReject(deliveryTag, false);
            }
        }
    }
}
